<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

class M_functions extends BZ_Model
{
/**
 * @section Settings
 */

	/**
	 * @function settings_get
	 * @description get the settings from database
	 * @status DEPRECATED, NOW USING THE CI CONFIG CLASS!! :)
	function setting_get($prm_dbase = 'db_default', $prm_table_name = 'biskitz_settings')
	{
		$use_db = $this->{$prm_dbase};

		$query = $use_db->get($prm_table_name);
		foreach ($query->result() as $rows)
		{
			$return_value[$rows->constant] = nl2br($rows->value);
		}


		return $return_value;
	}
	 */



/**
 * @section biskitZ Segment
 */

	/**
	 * @function segment_create()
	 * @description create the segment
	 */
	function segment_create($prm_segment_as_array = FALSE, $prm_segment_addition = NULL)
	{
		$return_value = $this->router->fetch_directory() . $this->router->fetch_class() . $prm_segment_addition;
		if ($prm_segment_as_array) $return_value = explode('/', $return_value);


		return $return_value;
	}



/**
 * @section Browser Functions
 */

	/**
	 * @function browser_supported
	 * @description allow the kind of browser to view the site
	 */
	function browser_supported()
	{
		$return_value = '';

		switch ($this->agent->browser())
		{
			case 'Internet Explorer':
				if ($this->agent->version() < 8)
				{
					$return_value = $this->lang->line('msg_browser_not_allowed');
				}
				break;
		}


		return $return_value;
	}



/**
 * @section E-mail Functions
 */

	/**
	 * @function email_send()
	 * @description send email
	 */
	function email_send($prm_from = 'webmaster@tahara.web.id', $prm_from_name = 'Webmaster', $prm_to = '', $prm_subject = '', $prm_message = '', $prm_bcc = '', $prm_mail_init = '')
	{
		/**
		 * @description initialize the mail config
		 */

		$mail_init = array
			(
				'useragent' => 'biskitZ - multipurpose cms'
/*
				, 'protocol' => 'smtp'
				, 'smtp_host' => 'smtp.budiluhur.ac.id'
				, 'smtp_port' => 25
				, 'smtp_user' => 'rizky.taharashita@budiluhur.ac.id'
				, 'smtp_pass' => 'rts2011'
				, 'mail_type' => 'html'
				, 'charset' => 'iso-8859-1'
				, 'wordwrap' => TRUE
*/
			);
		if (is_array($prm_mail_init))
		{
			$mail_init = array_merge($mail_init, $prm_mail_init);
		}


		$this->load->library('email', $mail_init);

		$this->email->clear();
		$this->email->from($prm_from, $prm_from_name);
		$this->email->to($prm_to);

		if (is_array($prm_bcc))
		{
			$this->email->bcc($prm_bcc);
		}

		$this->email->subject($prm_subject);
		$this->email->message($prm_message);

		$this->email->send();
	}



/**
 * @section File Functions
 */

	/**
	 * @function _file_delete()
	 * @description delete a file
	 */
	function _file_delete($prm_file_path = '', $prm_file_name = '')
	{
		$file_name = $prm_file_path . '/' . $prm_file_name;
		if (is_file($file_name) == TRUE) unlink($file_name);
	}


	/**
	 * @function file_delete()
	 * @param $prm_type: detail / master (detail only delete one file, master will delete files in the details [many files])
	 * @description delete file that get from a table, including the master data when it choose to be deleted
	 */
	function file_delete($prm_dbase = 'db_default', $prm_type = 'detail', $prm_table_name = '', $prm_table_where = '', $prm_fields = '*', $prm_file_path = '')
	{
		$data_file = $this->M_database->datas($prm_dbase, $prm_table_name, $prm_table_where, $prm_fields);

		if ($data_file)
		{
			switch ($prm_type)
			{
				case 'detail':
					$this->_file_delete($prm_file_path, $data_file[0][$prm_fields]);
					break;

				case 'master':
					foreach ($data_file as $rows)
					{
						$this->_file_delete($prm_file_path, $rows[$prm_fields]);
					}
					break;
			}
		}
	}


	/**
	 * @function filemanager_list
	 * @description show the files at the server from database to use by the cleditor server plugins
	 */
	function filemanager_list($prm_dbase = 'db_default', $prm_file_path = '', $prm_table_where = '', $prm_table_name = 'biskitz_front_content', $prm_table_fields = '*', $prm_fields_order = 'date desc, title')
	{
		$file_data = $this->M_database->datas
			(
				$prm_dbase
				, $prm_table_name
				, $prm_table_where
				, $prm_table_fields
				, FALSE
				, $prm_fields_order
				, ''
				, '', 0
				, FALSE
				, FALSE
			);

		if (is_array($file_data))
		{
			$file_list = '';
			foreach ($file_data as $files)
			{
				$file_list .= '<div><a onClick="$(\'#hidden_filesmanager\').val(\''.$prm_file_path.'/'.$files['file_name'].'\')" title="'.sprintf($this->lang->line('data_select_file'), $files['title']).'">';
				if (strchr($files['file_mime'], '/', TRUE) == 'image')
				{
					$file_list .= '<img src="' . $prm_file_path . '/' . $files['file_name'] . '" align="left" border="1" />';
				}
				$file_list .= '<b>' . $files['title'] . '</b><div align="right">' . $files['file_mime'] . '<br />' . $files['file_name'] . '</div></a></div>';
			}
		}
		else
		{
			$file_list = $this->lang->line('data_no_record');
		}


/*
		$return_value = '<div id="manager_bar"><div id="manager_navigator"><b>Select a file:</b></div><div id="manager_files">'.$file_list.'</div></div>';
*/
		$return_value = '<div id="manager_bar"><div id="manager_files">' . $file_list . '</div></div>';


		return $return_value;
	}



/**
 * @section Numbers Functions
 */

	/**
	 * @function numbers_to_voice
	 * @description read the numbers
	 */
	function numbers_to_voice($prm_sound_path, $prm_number_to_read)
	{
		$numbers = array
			(
				''
				, 'satu.mp3', 'dua.mp3', 'tiga.mp3', 'empat.mp3', 'lima.mp3'
				, 'enam.mp3', 'tujuh.mp3', 'delapan.mp3', 'sembilan.mp3', 'sepuluh.mp3'
				, 'sebelas.mp3'
			);

		if ($prm_number_to_read < 12)
		{
			if ($numbers[$prm_number_to_read] != '')
			{
				return $prm_sound_path . $numbers[$prm_number_to_read] . '|';
			}
		}
		elseif ($prm_number_to_read < 20)
		{
			return $this->numbers_to_voice($prm_sound_path, $prm_number_to_read - 10)
				. $prm_sound_path . 'belas.mp3|';
		}
		elseif ($prm_number_to_read < 100)
		{
			return $this->numbers_to_voice($prm_sound_path, $prm_number_to_read / 10)
				. $prm_sound_path . 'puluh.mp3|'
				. $this->numbers_to_voice($prm_sound_path, $prm_number_to_read % 10);
		}
		elseif ($prm_number_to_read < 200)
		{
			return $prm_sound_path . 'seratus.mp3|'
				. $this->numbers_to_voice($prm_sound_path, $prm_number_to_read - 100);
		}
		elseif ($prm_number_to_read < 1000)
		{
			return $this->numbers_to_voice($prm_sound_path, $prm_number_to_read / 100)
				. $prm_sound_path .'ratus.mp3|'
				. $this->numbers_to_voice($prm_sound_path, $prm_number_to_read % 100);
		}
		elseif ($prm_number_to_read < 2000)
		{
			return $prm_sound_path . 'seribu.mp3|'
				. $this->numbers_to_voice($prm_sound_path, $prm_number_to_read - 1000);
		}
		elseif ($prm_number_to_read < 1000000)
		{
			return $this->numbers_to_voice($prm_sound_path, $prm_number_to_read / 1000)
				. $prm_sound_path . 'ribu.mp3'
				. $this->numbers_to_voice($prm_sound_path, $prm_number_to_read % 1000);
		}
		elseif ($prm_number_to_read < 1000000000)
		{
			return $this->numbers_to_voice($prm_sound_path, $prm_number_to_read / 1000000)
				. $prm_sound_path . 'juta.mp3'
				. $this->numbers_to_voice($prm_sound_path, $prm_number_to_read % 1000000);
		}
	}



/**
 * @section Time Functions
 */

	/**
	 * @function micro_time
	 * @description format the current time to have a milliseconds
	 */
	function micro_time($format, $utimestamp = NULL)
	{
		if (is_null($utimestamp)) $utimestamp = microtime(true);
		$timestamp = floor($utimestamp);
		$milliseconds = round(($utimestamp - $timestamp) * 1000000);


		return date(preg_replace('`(?<!\\\\)u`', sprintf("%06u", $milliseconds), $format), $timestamp);
	}



/**
 * @section Array Functions
 */

	/**
	 * @function array_flatten_with_key
	 * @description flatten nested array with key
	 */
	function array_flatten_with_key(array $array)
	{
		$return_value = array();
		array_walk_recursive($array, function($a, $b) use (&$return_value) { $return_value[$b] = $a; });


		return $return_value;
	}


	/**
	 * @function array_flatten
	 * @description flatten nested array
	 */
	function array_flatten(array $array)
	{
		$return_value = array();
		if ( ! is_array($array)) return $return_value;

		foreach ($array as $value)
		{
			is_array($value)
			? $return_value = array_merge($return_value, $this->array_flatten($value))
			: array_push($return_value, $value);
		}


		return $return_value;
	}

}

/** End of file m_functions.php */
/** Location: ./biskitz_mpc/biskitz/models/m_functions.php */
